<?php

namespace App\Admin\Service;

use App\Admin\Grid\NGrid;
use App\Library\Models\AdminAuctionModel;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid\Filter;
use Encore\Admin\Show;
use Illuminate\Support\Facades\DB;

class AuctionService extends BaseService
{
    public string $table = 'auction';

    /**
     * grid
     * @return void
     */
    public function grid(): void
    {
        $this->grid = new NGrid(new AdminAuctionModel);
        $this->grid->model()
            ->where(function ($query) {
                if (!empty(Admin::user()->admin_type)) {
                    $mainId = Admin::user()->user_main_id;
                    $userIdArr[] = Admin::user()->user_id;
                    if (!empty($mainId)) {
                        $userIdArr = DB::table('shop_user')
                            ->where('user_main_id', $mainId)->pluck('user_id')->toArray();
                        $userIdArr[] = $mainId;
                    }
                    $query->whereIn('auction.publish_id', $userIdArr);
                }
            })
            ->with(['bid', 'publisher'])->orderByDesc('id');
        $this->filter();
        $this->gid();
    }

    /**
     * filter
     * @return void
     */
    public function filter(): void
    {
        $this->grid->filter(function (Filter $filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('id', 'ID')->integer();
                $filter->equal('sell_out', __('auction.sellOut'))->select(__('l.whether'));
                $filter->equal('is_start', __('auction.isStart'))->select(__('l.whether'));
                $filter->equal('audit_status', __('l.auditStatus'))->select(__('l.auditStatusArr'));
                $filter->gt('shot_count', __('auction.shotCount'));
                $filter->gt('start_price', __('auction.startPrice'));
                $filter->between('start_at', __('l.startAt'))->datetime();
                $filter->gt('inc', __('auction.inc'));
            });
            $filter->column(1 / 2, function (Filter $filter) {
                $filter->equal('status', __('l.status'))->select(__('auction.statusArr'));
                $filter->equal('expired', __('auction.expired'))->select(__('l.whether'));
                $filter->equal('is_end', __('auction.isEnd'))->select(__('l.whether'));
                $filter->equal('recommend', __('l.recommend'))->select(__('l.whether'));
                $filter->gt('user_count', __('auction.userCount'));
                $filter->gt('bail_price', __('auction.bailPrice'));
                $filter->between('end_at', __('l.endAt'))->datetime();
                $filter->between('created_at', __('l.createdAt'))->datetime();
            });
        });
    }

    /**
     * show
     * @param int $id
     * @return void
     */
    public function show(int $id): void
    {
        $this->id = $id;
        $this->show = new Show(AdminAuctionModel::findOrFail($id));
        $this->sid();
    }

    /**
     * form
     * @return void
     */
    public function form(): void
    {
        $this->form = new Form(new AdminAuctionModel);
    }

    /**
     * category
     * @return void
     */
    public function category(): void
    {
        $str = 'category_id_one';
        $array = DB::table('shop_category')->pluck('category_name', 'category_id')->toArray();
        if ($this->id === 0) {
            $this->grid->column($str, $this->l[$str])->display(function ($field) use ($array) {
                return categoryHelp($field, $array, $this);
            });
        } else {
            $this->show->field($str, $this->l[$str])->unescape()->as(function ($field) use ($array) {
                return categoryHelp($field, $array, $this);
            });
        }
    }

    /**
     * category
     * @param int $id
     * @return array
     */
    public function categoryArr(int $id): array
    {
        return DB::table('shop_category')->where('category_parent_id', $id)
            ->pluck('category_name', 'category_id')->toArray();
    }
}
